﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class DropDown : ICommandUIDefinition, IControlsChild, IParentable, IXmlBuildable, IChildDeletable
    {
        public string Alt { get; set; }
        public string AltArrow { get; set; }
        public bool? AutoComplete { get; set; }
        public int? AutoCompleteDelay { get; set; }
        public bool? CacheMenuVersions { get; set; }
        public string Command { get; set; }
        public string CommandMenuClose { get; set; }
        public string CommandMenuOpen { get; set; }
        public string CommandPreview { get; set; }
        public string CommandPreviewRevert { get; set; }
        public string Id { get; set; }
        public string InitialItem { get; set; }
        public bool? PopulateDynamically { get; set; }
        public bool? PopulateOnlyOnce { get; set; }
        public string PopulateQueryCommand { get; set; }
        public string QueryCommand { get; set; }
        public int? Sequence { get; set; }
        public string TemplateAlias { get; set; }
        public string ToolTipDescription { get; set; }
        public string ToolTipHelpKeyWord { get; set; }
        public string ToolTipImage32by32 { get; set; }
        public string ToolTipImage32by32Class { get; set; }
        public int? ToolTipImage32by32Left { get; set; }
        public int? ToolTipImage32by32Top { get; set; }
        public string ToolTipShortcutKey { get; set; }
        public string ToolTipTitle { get; set; }
        public string Width { get; set; }
        public Menu Menu { get; set; }

        public DropDown()
        {}

        public DropDown(XmlNode node)
        {
            try
            {
                this.Alt = XmlHelper.GetAttribute(node, "Alt");
                this.AltArrow = XmlHelper.GetAttribute(node, "AltArrow");
                this.AutoComplete = XmlHelper.GetAttribute(node, "AutoComplete") == "true";
                this.AutoCompleteDelay = XmlHelper.GetNullableIntAttribute(node, "AutoCompleteDelay");
                this.CacheMenuVersions = XmlHelper.GetAttribute(node, "CacheMenuVersions") == "true";
                this.Command = XmlHelper.GetAttribute(node, "Command");
                this.CommandMenuClose = XmlHelper.GetAttribute(node, "CommandMenuClose");
                this.CommandMenuOpen = XmlHelper.GetAttribute(node, "CommandMenuOpen");
                this.CommandPreview = XmlHelper.GetAttribute(node, "CommandPreview");
                this.CommandPreviewRevert = XmlHelper.GetAttribute(node, "CommandPreviewRevert");
                this.Id = XmlHelper.GetAttribute(node, "Id");
                this.InitialItem = XmlHelper.GetAttribute(node, "InitialItem");
                this.PopulateDynamically = XmlHelper.GetAttribute(node, "PopulateDynamically") == "true";
                this.PopulateOnlyOnce = XmlHelper.GetAttribute(node, "PopulateOnlyOnce") == "true";
                this.PopulateQueryCommand = XmlHelper.GetAttribute(node, "PopulateQueryCommand");
                this.QueryCommand = XmlHelper.GetAttribute(node, "QueryCommand");
                this.Sequence = (int?)XmlHelper.ExtractXmlAttributeToProperty2(node, "Sequence", XmlHelper.PropertyType.integerType);
                this.TemplateAlias = XmlHelper.GetAttribute(node, "TemplateAlias");
                this.ToolTipDescription = XmlHelper.GetAttribute(node, "ToolTipDescription");
                this.ToolTipHelpKeyWord = XmlHelper.GetAttribute(node, "ToolTipHelpKeyWord");
                this.ToolTipImage32by32 = XmlHelper.GetAttribute(node, "ToolTipImage32by32");
                this.ToolTipImage32by32Class = XmlHelper.GetAttribute(node, "ToolTipImage32by32Class");
                this.ToolTipImage32by32Left = (int?)XmlHelper.ExtractXmlAttributeToProperty2(node, "ToolTipImage32by32Left", XmlHelper.PropertyType.integerType);
                this.ToolTipImage32by32Top = (int?)XmlHelper.ExtractXmlAttributeToProperty2(node, "ToolTipImage32by32Top", XmlHelper.PropertyType.integerType);
                this.ToolTipShortcutKey = XmlHelper.GetAttribute(node, "ToolTipShortcutKey");
                this.ToolTipTitle = XmlHelper.GetAttribute(node, "ToolTipTitle");
                this.Width = XmlHelper.GetAttribute(node, "Width");

                if (node.SelectSingleNode("Menu") != null)
                {
                    this.Menu = new Menu(node.SelectSingleNode("Menu"));
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode customActionNode)
        {
            try
            {
                XmlNode dropDownNode = XmlHelper.AddNode(customActionNode, "DropDown");
                XmlHelper.AddAttribute(dropDownNode, "Alt", this.Alt);
                XmlHelper.AddAttribute(dropDownNode, "AltArrow", this.AltArrow);
                XmlHelper.AddAttribute(dropDownNode, "AutoComplete", this.AutoComplete);
                XmlHelper.AddAttribute(dropDownNode, "AutoCompleteDelay", this.AutoCompleteDelay);
                XmlHelper.AddAttribute(dropDownNode, "CacheMenuVersions", this.CacheMenuVersions);
                XmlHelper.AddAttribute(dropDownNode, "Command", this.Command);
                XmlHelper.AddAttribute(dropDownNode, "CommandMenuClose", this.CommandMenuClose);
                XmlHelper.AddAttribute(dropDownNode, "CommandMenuOpen", this.CommandMenuOpen);
                XmlHelper.AddAttribute(dropDownNode, "CommandPreview", this.CommandPreview);
                XmlHelper.AddAttribute(dropDownNode, "CommandPreviewRevert", this.CommandPreviewRevert);
                XmlHelper.AddAttribute(dropDownNode, "Id", this.Id);
                XmlHelper.AddAttribute(dropDownNode, "InitialItem", this.InitialItem);
                XmlHelper.AddAttribute(dropDownNode, "PopulateDynamically", this.PopulateDynamically);
                XmlHelper.AddAttribute(dropDownNode, "PopulateOnlyOnce", this.PopulateOnlyOnce);
                XmlHelper.AddAttribute(dropDownNode, "PopulateQueryCommand", this.PopulateQueryCommand);
                XmlHelper.AddAttribute(dropDownNode, "QueryCommand", this.QueryCommand);
                XmlHelper.AddAttribute(dropDownNode, "Sequence", this.Sequence);
                XmlHelper.AddAttribute(dropDownNode, "TemplateAlias", this.TemplateAlias);
                XmlHelper.AddAttribute(dropDownNode, "ToolTipDescription", this.ToolTipDescription);
                XmlHelper.AddAttribute(dropDownNode, "ToolTipHelpKeyWord", this.ToolTipHelpKeyWord);
                XmlHelper.AddAttribute(dropDownNode, "ToolTipImage32by32", this.ToolTipImage32by32);
                XmlHelper.AddAttribute(dropDownNode, "ToolTipImage32by32Class", this.ToolTipImage32by32Class);
                XmlHelper.AddAttribute(dropDownNode, "ToolTipImage32by32Left", this.ToolTipImage32by32Left);
                XmlHelper.AddAttribute(dropDownNode, "ToolTipImage32by32Top", this.ToolTipImage32by32Top);
                XmlHelper.AddAttribute(dropDownNode, "ToolTipShortcutKey", this.ToolTipShortcutKey);
                XmlHelper.AddAttribute(dropDownNode, "ToolTipTitle", this.ToolTipTitle);
                XmlHelper.AddAttribute(dropDownNode, "Width", this.Width);

                if (this.Menu != null)
                {
                    this.Menu.AddNode(dropDownNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("DropDown");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, new string[] { "Menu" }, true);

                if (this.Menu != null)
                {
                    node.ContextMenuStrip.Items[0].Enabled = false;
                    this.Menu.BuildTreeViewElement(node);
                }

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IParentable Members

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                if (!(child is Menu))
                {
                    throw new Exception("Unsupported child type: " + child.GetType().ToString());
                }
                else
                {
                    this.Menu = (Menu)child;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is Menu)
                {
                    this.Menu = null;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
